﻿var req;
var resultPanelID = "";
var strLoadedModule = "";

function Initialize()
{
	try
	{
		req=new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e)
	{
		try
		{
			req=new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch(oc)
		{
			req=null;
		}
	}

	if(!req&&typeof XMLHttpRequest!="undefined")
	{
		req=new XMLHttpRequest();
	}
	
}

function SendQuery(tr, mr, nr)
{
	
	resultPanelID = "MB-" + mr;	
	if (strLoadedModule.indexOf('#' + resultPanelID + '#') >= 0) return;
		
	Initialize();
	var urlPrefix = window.location.href;
	urlPrefix = urlPrefix.substr(0, urlPrefix.lastIndexOf("/") + 1);
	var url= urlPrefix + "ProcessModule.aspx";
	
	try
	{	
		if(req!=null)
		{
			if (tr != null && tr != "") url += "?TabRef=" + encodeURIComponent(tr);
			if (mr != null && mr != "") url += "&ModuleRef=" + encodeURIComponent(mr);
			if (nr != null)
			{
				nr = nr == "" ? "0" : nr;
				url += "&NewsRef=" + encodeURIComponent(nr);
			}
			req.onreadystatechange = Process;
			//alert(url);
			req.open("GET", url, false);
			/*req.open("POST", url, false);
			if (tr != null && tr != "") req.setRequestHeader("TR", encodeURIComponent(tr));
			if (mr != null && mr != "") req.setRequestHeader("MR", encodeURIComponent(mr));
			if (nr != null)
			{
				nr = nr == "" ? "0" : nr;
				req.setRequestHeader("NR", encodeURIComponent(nr));
			}*/
			req.send(null);
		}
	}
	catch(e)
	{
	}
	
}

function replacestring(str_normal,str_find,str_replace,int_case_insensitive)
{
	if (arguments.length<3 || str_find=="" || str_normal=="" || typeof("".split)!="function")
		return(str_normal);

	//no parm means default, "case SENSITIVE"...
	if(!(int_case_insensitive))
		return(str_normal.split(str_find)).join(str_replace);

	str_find=str_find.toLowerCase();

	var rv="";
	var ix=str_normal.toLowerCase().indexOf(str_find);
	while(ix>-1)
	{
		rv+=str_normal.substring(0,ix)+str_replace;
		str_normal=str_normal.substring(ix+str_find.length);
		ix=str_normal.toLowerCase().indexOf(str_find);
	};
	return(rv+str_normal);
};//end function replacestring



function Process()
{
	if (req.readyState == 4) 
        {
        // only if "OK"
			if (req.status == 200) 
			{
				if(req.responseText=="")
				{
					//HideDiv(resultPanelID);
				}
				else
				{
					try
					{
						var _intStartTrace = req.responseText.indexOf('<StartModuleTrace>') + 18;
						var _intEndTrace = req.responseText.indexOf('</StartModuleTrace>');
						var _strModuleContent = req.responseText.substring(_intStartTrace, _intEndTrace);
						_strModuleContent = replacestring(_strModuleContent, '<Script', '<script', false);
						
						var _intScriptTrace  = _strModuleContent.indexOf('<script');
						var _intEndScriptTrace = _strModuleContent.indexOf('</script>', _intScriptTrace);
						var _intSrcPos = _strModuleContent.indexOf('src="', _intScriptTrace);
						var _intLinkTrace  = _strModuleContent.indexOf('<link');
						
						var _strScripts = '';
						var _strCheckScriptTag = '';
						var _strCheckLinkTag = '';						
						//alert(_strModuleContent);
						
						if (_intLinkTrace >= 0)
						{
							_strCheckLinkTag = _strModuleContent.substring(_intLinkTrace, _strModuleContent.indexOf('>', _intLinkTrace))
							if (_strCheckLinkTag.length > 0)
							{
								var _intStartHref = _strCheckLinkTag.indexOf('href="') + 6;
								var _intEndHref = _strCheckLinkTag.indexOf('"', _intStartHref);
								_strCheckLinkTag = _strCheckLinkTag.substring(_intStartHref, _intEndHref);								
								loadLink(_strCheckLinkTag);								
							}
						}
						//alert(_strCheckLinkTag);
						while (_intScriptTrace >= 0 && _intEndScriptTrace > _intScriptTrace && ((_intSrcPos > _intScriptTrace)&&(_intSrcPos < _intEndScriptTrace)))
						{							
							_strCheckScriptTag = _strModuleContent.substring(_intScriptTrace, _strModuleContent.indexOf('>', _intScriptTrace))
							if (_strCheckScriptTag.indexOf('src') > 0)
							{
								var _intStartSrc = _strCheckScriptTag.indexOf('src="') + 5;
								var _intEndSrc = _strCheckScriptTag.indexOf('"', _intStartSrc);
								_strCheckScriptTag = _strCheckScriptTag.substring(_intStartSrc, _intEndSrc);
								if (_strCheckScriptTag.length > 0)
								{														
									loadScript(_strCheckScriptTag);									
									_strModuleContent = _strModuleContent.substring(0, ((_intScriptTrace == 0) ? _intScriptTrace : (_intScriptTrace))) + _strModuleContent.substr(_intEndScriptTrace + 9);
									//alert(_strModuleContent);									
								}
							}						
							
							_intScriptTrace  = _strModuleContent.indexOf('<script');
							_intSrcPos = _strModuleContent.indexOf('src="', _intScriptTrace);
							_intEndScriptTrace = _strModuleContent.indexOf('</script>', _intScriptTrace);
							
							/*alert("_intScriptTrace = " + _intScriptTrace);
							alert("_intSrcPos = " + _intSrcPos);
							alert("_intEndScriptTrace = " + _intEndScriptTrace);*/
						}						
						//alert(_strModuleContent);
						document.getElementById(resultPanelID).innerHTML = _strModuleContent;
						_intScriptTrace  = _strModuleContent.indexOf('<script');
						_intEndScriptTrace = _strModuleContent.indexOf('</script>', _intScriptTrace);
						while (_intScriptTrace >= 0 && _intEndScriptTrace > _intScriptTrace)
						{
							/*alert("_intScriptTrace = " + _intScriptTrace);
							alert("_intEndScriptTrace = " + _intEndScriptTrace);	
							alert("_strModuleContent.indexOf('>', _intScriptTrace) + 1 = " + (_strModuleContent.indexOf('>', _intScriptTrace) + 1));*/
							if (_strModuleContent.indexOf('>', _intScriptTrace) + 1 < _intEndScriptTrace)
							{
								_strScripts = _strModuleContent.substring(_strModuleContent.indexOf('>', _intScriptTrace) + 1, _intEndScriptTrace - 1);
								//alert(_strScripts);
								try
								{
									if (_strScripts.length > 0) eval(_strScripts);
								}
								catch(EvalScriptError)
								{
									//alert('Error : ' + EvalScriptError.description + '\nScripts : \n' + _strScripts);
								}
							}
							
							_strModuleContent = _strModuleContent.substring(0, ((_intScriptTrace == 0) ? _intScriptTrace : (_intScriptTrace - 1))) + _strModuleContent.substr(_intEndScriptTrace + 9);
							_intScriptTrace  = _strModuleContent.indexOf('<script');
							_intEndScriptTrace = _strModuleContent.indexOf('</script>', _intScriptTrace);
						}
						strLoadedModule += '#' + resultPanelID + '#';
						
						req.abort();
					}
					catch(e)
					{
						//alert(e.description + '\n' + e.line + '\n' + e.message );
					}
				}
			}
			else 
			{
				document.getElementById(resultPanelID).innerHTML="<font style=\"font : Verdana 11px;\">Lỗi xảy ra : " + req.statusText + "</font>";
			}
		}
}

function loadScript (url) {
  if (document.layers)
    window.location.href = url;
  else if (document.getElementById) {
    var script = document.createElement('script');
    script.defer = true;
    script.src = url;
    script.type = "text/javascript";
    document.getElementsByTagName('head')[0].appendChild(script);    
  }
  
  /*document.open();
  document.write('<script src="', url, '" type="text/JavaScript"><\/script>');  
  document.close();*/
}

function loadLink (url) {
  if (document.layers)
    window.location.href = url;
  else if (document.getElementById) {
    var link = document.createElement('link');    
    link.href = url;
    link.rel = "stylesheet";
    link.type = "text/css";
    document.getElementsByTagName('head')[0].appendChild(link);
  }
}
